package db;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.hutoubenjg.R;

import java.util.ArrayList;

import activity.addressmanage.AddressDetailActivity;
import adapter.AreaAdapter;
import base.BaseActivity;


/**
 * 区
 */
public class AraeActivity extends BaseActivity {
    TextView tvTitle, tvTest;
    ImageView back;
    ListView city_listview;
    private SQLiteDatabase database;
    private ArrayList<CitysBean> litArae;// 区域列表
    AreaAdapter adapter;
    private CityDBManager cityDBManager;
    String cityId;//上个页面传过来的城市id
    private String cityName;//上个页面传过来的省id
    private String provinceName;//上个页面传过来的省name
    String type;
    String id01;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_arae);
        tvTitle.setVisibility(View.GONE);
        tvTest.setText("选择地址");
        cityId=getIntent().getStringExtra("cityId");
        type=getIntent().getStringExtra("type");
        id01=this.getIntent().getStringExtra("id");
        cityName=getIntent().getStringExtra("cityName");
        provinceName=getIntent().getStringExtra("provinceName");
        cityDBManager = new CityDBManager(this);
        cityDBManager.openDateBase();
        database = SQLiteDatabase.openOrCreateDatabase(
                CityDBManager.DB_PATH + "/" + CityDBManager.DB_NAME,null);

        litArae = cityDBManager.queryArea(cityId);
        adapter=new AreaAdapter(this,litArae);
        city_listview.setAdapter(adapter);

        city_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(AraeActivity.this,AddressDetailActivity.class);
                String  arerName=litArae.get(position).getAreaName();
                intent.putExtra("provinceName",provinceName);
                intent.putExtra("cityName",cityName);
                intent.putExtra("arerName",arerName.equals("全部")||arerName.equals("不限")?"":arerName);
                intent.putExtra("type",type);
                intent.putExtra("name",getIntent().getStringExtra("name"));
                intent.putExtra("mobile",getIntent().getStringExtra("mobile"));
                intent.putExtra("youbian",getIntent().getStringExtra("youbian"));
                intent.putExtra("street",getIntent().getStringExtra("street"));
                intent.putExtra("default",getIntent().getStringExtra("default"));
                intent.putExtra("id",id01);
                startActivity(intent);

                // 发送广播通知LoginActivity结束
                Intent intent1 = new Intent(AddressDetailActivity.ADDRESSEXIT);
                sendBroadcast(intent1);
                finish();
            }
        });
    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }
}
